<?php

/**
 * DAO for inserting Notifications into the database.
 */
class NotificationDAO {
    
    public function insert(Notification $notification){
        // Get database connection
        $db = @new MySQLi(Configuration::$DB_HOST, Configuration::$DB_USER, Configuration::$DB_PASS, Configuration::$DB_NAME);
        
        // Notification Type is hard coded for now. 1 is for Notification.
        $query = 'INSERT INTO notification ';
        $query .= '(NOTIFICATION_TYPE, title, content, creator, important) VALUES ';
        $query .= '(1, \''.mysqli_real_escape_string($db, $notification->getTitle()).'\', \''.mysqli_real_escape_string($db, $notification->getContent()).'\', \''.mysqli_real_escape_string($db, $notification->getCreator()).'\', \''.mysqli_real_escape_string($db, $notification->getImportant()).'\');';
        
        return mysqli_query($db, $query) ? mysqli_insert_id($db) : false;
    }
    
    public function update(Notification $notification){
        // Get database connection
        $db = @new MySQLi(Configuration::$DB_HOST, Configuration::$DB_USER, Configuration::$DB_PASS, Configuration::$DB_NAME);
        
        // Notification Type is hard coded for now. 1 is for Notification.
        $query = 'UPDATE notification SET title = \''.mysqli_real_escape_string($db, $notification->getTitle()).'\',';
        $query .= 'content = \''.mysqli_real_escape_string($db, $notification->getContent()).'\',';
        $query .= 'creator = \''.mysqli_real_escape_string($db, $notification->getCreator()).'\',';
        $query .= 'important = \''.mysqli_real_escape_string($db, $notification->getImportant()).'\',';
        $query .= 'modified = \''.mysqli_real_escape_string($db, $notification->getModified()).'\',';
        $query .= 'archived = \''.mysqli_real_escape_string($db, $notification->getArchived()).'\' ';
        $query .= 'WHERE NID = '.mysqli_real_escape_string($db, $notification->getId()).';';
        
        return mysqli_query($db, $query);
    }
    
    /**
     * Get all currently active Notification items.
     * 
     * Deleted and archived items will not be returned.
     * Items are ordered by priority and date.
     * 
     * @return type 
     */
    public function getAll($importantfirst = true, $archived = false, $deleted = false){
        $db = @new MySQLi(Configuration::$DB_HOST, Configuration::$DB_USER, Configuration::$DB_PASS, Configuration::$DB_NAME);

        $resultset = array();
        
        $query = "SELECT * FROM notification WHERE NOTIFICATION_TYPE = 1 ";
        if(!$deleted){
            $query .= "AND deleted = 0 ";
        }
        if(!$archived){
            $query .= "AND archived = 0 ";
        }
        if($importantfirst){
            $query .= "ORDER BY important DESC, added DESC;";
        } else {
            $query .= "ORDER BY added DESC;";
        }
        
        $result = mysqli_query($db, $query);
        
        echo mysqli_error($db);
        
        while($row = mysqli_fetch_assoc($result)){
            $resultset[] = $this->createFromRow($row);
        }
        
        return $resultset;
    }
    
    /**
     * Get single notification by ID.
     * 
     * @param int $id ID of the required notification.
     * @return Notification 
     */
    public function get($id){
        $db = @new MySQLi(Configuration::$DB_HOST, Configuration::$DB_USER, Configuration::$DB_PASS, Configuration::$DB_NAME);

        $resultset = array();
        
        $id = mysqli_real_escape_string($db, $id);
        
        $query = "SELECT * FROM notification WHERE NOTIFICATION_TYPE = 1 AND NID = $id;";
        $result = mysqli_query($db, $query);
        
        echo mysqli_error($db);
        
        while($row = mysqli_fetch_assoc($result)){
            $resultset[] = $this->createFromRow($row);
        }
        
        return count($resultset) > 0 ? $resultset[0] : null;
    }
    
    private function createFromRow($row){
        $notification = new Notification();
        $notification->setAdded($row['added']);
        $notification->setArchived($row['archived']);
        $notification->setContent($row['content']);
        $notification->setCreator($row['creator']);
        $notification->setDeleted($row['deleted']);
        $notification->setEditor($row['editor']);
        $notification->setId($row['NID']);
        $notification->setImportant($row['important']);
        $notification->setModified($row['modified']);
        $notification->setTitle($row['title']);
        $notification->setType($row['NOTIFICATION_TYPE']);
        
        return $notification;
    }
}

?>
